home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / QuakeTools / src / libqdisplay / tbsp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  3.4 KB  |  127 lines

  1. #define    LIBQDISPLAY_CORE
  2. #include "../include/libqdisplay.h"
  3.  
  4. /*
  5.  * model: a combination of a brush and an entity.
  6.  * 
  7.  * One or more brushes are bound to an entity,
  8.  * which controls the behavior of the brushes.
  9.  * All brushes are contained within models.
  10.  * 
  11.  * The model numbers in the compiled BSP (*x) comes from the order in which
  12.  * the models are stored in the models structure. These numbers are originally
  13.  * derived from the order of the models in the source MAP file.
  14.  * 
  15.  * The worldspawn model is a bounding box that defines the extents of the
  16.  * whole world.
  17.  * 
  18.  * The models are defined by a bounding box of the max and min(x,y,z).
  19.  * Therefore they are always parallel to the horizontal planes.
  20.  * -- (qkspecs.)
  21.  */
  22.  
  23. /*
  24.  * 
  25.  */
  26. int point_plane_test(struct dplane_t * plane)
  27. {
  28.   switch(plane->type) {
  29.     case PLANE_X:
  30.       return cameraLocation[0] < plane->dist;
  31.       break;
  32.     case PLANE_Y:
  33.       return cameraLocation[1] < plane->dist;
  34.       break;
  35.     case PLANE_Z:
  36.       return cameraLocation[2] < plane->dist;
  37.       break;
  38.     default:
  39.       return DotProduct(plane->normal, cameraLocation) < plane->dist;
  40.       break;
  41.   }
  42. }
  43.  
  44. /*
  45.  * there could be three different values in children[]
  46.  *  -1        terminator, references to dleaf[0]    is set if childrens planenum == -1 and CONTENTS_SOLID
  47.  *  negative    what follows are leafs            is set otherwise
  48.  *  positive    what follows are nodes            is set if childrens planenum != -1
  49.  */
  50.  
  51. int find_leaf(__memBase)
  52. {
  53.   int n = bspMem->dmodels[model].headnode[0];
  54.   struct dnode_t *node;
  55.  
  56.   do {
  57.     node = &bspMem->dnodes[n];
  58.   } while ((n = node->children[point_plane_test(&bspMem->dplanes[node->planenum])]) >= 0);
  59.   
  60.   return ~n;
  61. }
  62.  
  63. void bsp_render_node(__memBase, int node)
  64. {
  65.   if (node >= 0) {
  66.     if(is_marked_node(node)) {
  67.       if (point_plane_test(&bspMem->dplanes[bspMem->dnodes[node].planenum])) {
  68.         bsp_render_node(bspMem, bspMem->dnodes[node].children[0]);
  69.         render_node_faces(bspMem, node, 1);
  70.         bsp_render_node(bspMem, bspMem->dnodes[node].children[1]);
  71.       }
  72.       else {
  73.         bsp_render_node(bspMem, bspMem->dnodes[node].children[1]);
  74.         render_node_faces(bspMem, node, 0);
  75.         bsp_render_node(bspMem, bspMem->dnodes[node].children[0]);
  76.       }
  77.       unmark_node(node);
  78.     }
  79.   }
  80. }
  81.  
  82. void bsp_explore_node(__memBase, int node)
  83. {
  84.   if (node >= 0) {
  85.     if (is_marked_node(node)) {
  86.       if (!node_in_frustrum(&bspMem->dnodes[node])) {        // this is absolutely bogus, for we use sphere-culling, not frustum-culling
  87.         unmark_node(node);
  88.       }
  89.       else {
  90.         bsp_explore_node(bspMem, bspMem->dnodes[node].children[0]);
  91.         bsp_explore_node(bspMem, bspMem->dnodes[node].children[1]);
  92.       }
  93.     }
  94.   }
  95.   else {
  96.     /*
  97.      * node is 0 or a valid leaf-node
  98.      * if 0 everything is invisible (leaf 0 is CONTENTS_SOLID)
  99.      * else it is something other
  100.      */
  101.     node = ~node;
  102.     
  103.     if (is_marked_leaf(node))
  104.       if (leaf_in_frustrum(&bspMem->dleafs[node]))
  105.         mark_leaf_faces(bspMem, node);
  106.   }
  107. }
  108.  
  109. // recursively determine which nodes need exploring (so we
  110. // don't look for polygons on _every_ node in the level)
  111. //
  112. // this need only be called once at the beginning
  113. int bsp_find_visible_nodes(__memBase, int node)
  114. {
  115.   if (node >= 0) {
  116.     if(bsp_find_visible_nodes(bspMem, bspMem->dnodes[node].children[0]) |
  117.        bsp_find_visible_nodes(bspMem, bspMem->dnodes[node].children[1]))
  118.       return mark_node(node);
  119.     else
  120.       return 0;
  121.   }
  122.   else {
  123.     node = ~node;
  124.     return is_marked_leaf(node);
  125.   }
  126. }
  127.